package hu.mapro.rollerx.core;

import hu.mapro.rollerx.core.FractalTerrain.Vertices;

import com.badlogic.gdx.math.Vector2;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.operation.buffer.BufferParameters;

public class Ground3 extends AbstractGround {

	public Ground3() {
		super(true, true);
	}
	
	void generateFractal(float carWidth) {
		fractalTerrain = new FractalTerrain();
		fractalTerrain.x1 = 0;
		fractalTerrain.x2 = 150;
		fractalTerrain.amplitude = 25;
		fractalTerrain.levels = 6;
		fractalTerrain.denominator = 2;
		
		vertices = new Vertices(fractalTerrain.getVertexCount() + 2);
		
		float platformWidth = getPlatformWidth(carWidth);
		
		fractalTerrain.randomizeEndpoints();
		
		vertices.addVertex(-platformWidth, fractalTerrain.y1);

		fractalTerrain.generateFractal(vertices);
		
		vertices.addVertex(fractalTerrain.x2+platformWidth, fractalTerrain.y2);
	}

	static float getPlatformWidth(float carWidth) {
		return carWidth*2;
	}
	
	Vertices vertices;
	private FractalTerrain fractalTerrain;
	
	@Override
	Vector2[][] generateVertices(float carWidth, float carHeight) {
		generateFractal(carWidth);
		
		Coordinate[] coords = new Coordinate[vertices.terrainVertices.length/2];
		for (int i=0;i<coords.length;i++) {
			coords[i] = new Coordinate(vertices.terrainVertices[i*2], vertices.terrainVertices[i*2+1]);
		}
		BufferParameters bufferParameters = new BufferParameters();
		bufferParameters.setEndCapStyle(BufferParameters.CAP_FLAT);
		bufferParameters.setJoinStyle(BufferParameters.JOIN_BEVEL);
		Vector2[][] cave = Grounds.createCave(coords, bufferParameters, getHeight(carHeight)/2);
		return cave;
	}

	@Override
	public Vector2 getCarPosition(float carWidth, float carHeight) {
		return new Vector2(-carWidth, fractalTerrain.y1-getHeight(carHeight)/2);
	}

	float getHeight(float carHeight) {
		return carHeight*2;
	}
	
	
}
